function scrollTop(){
	$('html, body').animate({scrollTop:0}, 'slow');
}

function showLoader(status){
    if(status){
        $("#ajaxBusy").show();
       
        $("#panelBackground").css({
		  position:'fixed', //or fixed depending on needs 
		  left: 0,
		  height: $(document).height() * 1.5,
                  margin:'-250px 0 0 0',
		  width: '100%',
		  display: 'block'
	});
        $("#reisduur-range").css({
            opacity: 0.5
        })
        $("#heenreis-range").css({
            opacity: 0.5
        })
         $("#terugreis-range").css({
            opacity: 0.5
        })
    }else{
        $("#ajaxBusy").hide();
        $("#panelBackground").hide();
        $("#reisduur-range").css({
            opacity: 1
        })
        $("#heenreis-range").css({
            opacity: 1
        })
         $("#terugreis-range").css({
            opacity: 1
        })
    }
}

function get_XmlHttp() {
  // create the variable that will contain the instance of the XMLHttpRequest object (initially with null value)fdsa
  var xmlHttp = null;

  if(window.XMLHttpRequest) {		// for Forefox, IE7+, Opera, Safari, ...
    xmlHttp = new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {	// for Internet Explorer 5 or 6
    xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  }

  return xmlHttp;
}

function showPopup(emId) {
	
	 var topPos = $("#flight-"+emId).position();
         
         showLoader(true);
         $("#ajaxBusy").hide();
	$("#bookingPanelPopup").css("position","absolute");
	$("#bookingPanelPopup").css("display","block");
	$("#bookingPanelPopup").css("width","980px");
	$("#bookingPanelPopup").css("top", Math.max(0, (($(window).height() - 	$("#bookingPanelPopup").outerHeight()) /  30) +   $(window).scrollTop()) + "px");
	$("#bookingPanelPopup").css("left","120.1px");
	


	var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance

	var  url = "flightDetails.php"+'?id=' + emId;

	  request.open("GET", url, true);			// define the request
	  request.send(null);		// sends data

	  // Check request status
	  // If the response is received completely, will be transferred to the HTML tag with tagID
	  request.onreadystatechange = function() {
		if (request.readyState == 4) {
			  document.getElementById("bookingPanelPopup").innerHTML = request.responseText;
		}
	  }

 }

 
// sends data to a php file, via GET, and displays the received answer
function reistijd(tagID, heentijd,terugtijd) {
	
  var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance

  // create the URL with data that will be sent to the server (a pair index=value)
  var  url = "ajaxHandler.php"+'?reistijd=true&heentijd=' + heentijd +'&terugtijd=' + terugtijd;

  request.open("GET", url, true);			// define the request
  request.send(null);		// sends data

  // Check request status
  // If the response is received completely, will be transferred to the HTML tag with tagID
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
		  document.getElementById(tagID).innerHTML = request.responseText;
		   $('.pagination').each(function(index) {
			this.style.backgroundColor = "#303030";
		   });
		  document.getElementById(page).style.backgroundColor = "#f25900";
    }
  }
}


// sends data to a php file, via GET, and displays the received answer
function nextPage(tagID, page) {
	
  var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance
  showLoader(true);
   scrollTop();
  // create the URL with data that will be sent to the server (a pair index=value)
  var  url = "ajaxHandler.php"+'?page=' + page;

  request.open("GET", url, true);			// define the request
  request.send(null);		// sends data

  // Check request status
  // If the response is received completely, will be transferred to the HTML tag with tagID
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
		  document.getElementById(tagID).innerHTML = request.responseText;
                 
		  $('.pagination').each(function(index) {
			this.style.backgroundColor = "#303030";
		   });
                    showLoader(false);
		  document.getElementById(page).style.backgroundColor = "#f25900";
    }
  }
}

function heenreis(tagID,begintijd,terugtijd) {

  var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance

  // create the URL with data that will be sent to the server (a pair index=value)
  var  url = "ajaxHandler.php"+'?heenreis=true&begintijd=' + begintijd + '&terugtijd=' + terugtijd;
 showLoader(true);
  request.open("GET", url, true);			// define the request
  request.send(null);		// sends data

  // Check request status
  // If the response is received completely, will be transferred to the HTML tag with tagID
  request.onreadystatechange = function() {
  
	if (request.readyState == 4) {
		  document.getElementById(tagID).innerHTML = request.responseText;
		  $('.pagination').each(function(index) {
			this.style.backgroundColor = "#303030";
		   });
                    showLoader(false);
		  document.getElementById(page).style.backgroundColor = "#f25900";
    }
  }
}


function terugreis(tagID,begintijd,terugtijd) {

  var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance

  // create the URL with data that will be sent to the server (a pair index=value)
  var  url = "ajaxHandler.php"+'?terugreis=true&begintijd=' + begintijd + '&terugtijd=' + terugtijd;

  request.open("GET", url, true);			// define the request
  request.send(null);		// sends data
 showLoader(true);
  // Check request status
  // If the response is received completely, will be transferred to the HTML tag with tagID
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
		  document.getElementById(tagID).innerHTML = request.responseText;
		  $('.pagination').each(function(index) {
			this.style.backgroundColor = "#303030";
		   });
                    showLoader(false);
		  document.getElementById(page).style.backgroundColor = "#f25900";
    }
  }
}

function disableStop(stop,tagID) {

   showLoader(true);
  var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance

  // create the URL with data that will be sent to the server (a pair index=value)
  //var  url = serverPage+'?page=' + page;
  var url = "ajaxHandler.php?stop=" + stop;

  request.open("GET", url, true);			// define the request
  request.send(null);		// sends data

  // Check request status
  // If the response is received completely, will be transferred to the HTML tag with tagID
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
		  document.getElementById(tagID).innerHTML = request.responseText;
		  buildPagination();
		  showLoader(false);
    }
  }

}

function disableCarrier(carrier,tagID) {

   showLoader(true);
  var request =  get_XmlHttp();		// call the function for the XMLHttpRequest instance

  // create the URL with data that will be sent to the server (a pair index=value)
  //var  url = serverPage+'?page=' + page;
  var url = "ajaxHandler.php?carrier=" + carrier;

  request.open("GET", url, true);			// define the request
  request.send(null);		// sends data

  // Check request status
  // If the response is received completely, will be transferred to the HTML tag with tagID
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
         showLoader(false);
		  document.getElementById(tagID).innerHTML = request.responseText;
		  buildPagination();
		 
    }
  }

}